// pages/mes/commit-compony-auth/commit-compony-auth.js
import Toast from '../../../component/toast/toast';
import utils from '../../../utils/util.js';
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        authChecked: false,
        authName: '',
        authJob: '',
        authIdNum: '',
        authphoto: '',
        enterpriseInfo: {}
    },
    /**
     * ===================事件处理部分===================
     */
    toAgreement(){
        app.globalData.globalWebUrl = app.globalData.agreementUrl;
        wx.navigateTo({
            url: '/pages/agreement-web/agreement-web?title=闲将信息发布规范'
        })
    },
    uploadImg() {
        wx.navigateTo({
            url: '/pages/compony-pages/user-card/user-card'
        })
    },
    checkboxChange() {
        let currentCheck = this.data.authChecked;
        this.setData({
            authChecked: !currentCheck
        });
    },
    onClick(e) {
        let index = e.currentTarget.dataset.index;
        wx.navigateTo({
            url: '/pages/compony-pages/auth-name/auth-name?flag=' + index
        })
    },
    commitAuth() {
        if (!this.data.authName) {
            Toast("请填写认证人姓名！");
            return;
        }
        if (!this.data.authJob) {
            Toast("请填写认证人职位！");
            return;
        }
        if (!this.data.authIdNum) {
            Toast("请填写认证人身份证号码！");
            return;
        }
        if (!this.data.authIdNum) {
            Toast("请上传认证人证件照片！");
            return;
        }
        // if (!this.data.authChecked) {
        //     Toast("请先阅读协议！");
        //     return;
        // }
        this.addEnterpriseInfo(this.joinData());
    },
    joinData() {
        let enterpriseInfo = this.data.enterpriseInfo;
        let data = {
            "Id": "string",
            "UserInfoId": "string",
            "EnterpriseName": "string",
            "BusinessLicense": "string",
            "Industry": "string",
            "Scale": "string",
            "FinancingStage": "string",
            "CertifierName": "string",
            "CertifierPosition": "string",
            "CertifierIdNumber": "string",
            "CertifierFrontPhoto": "string",
            "CertifierBackPhoto": "string",
            "CertifierHoldingPhoto": "string",
            "VerifyStatus": 0,
            "VerifyTime": utils.formatTime(new Date()),
            "Reason": "string"
        };
        data.Id = '';
        data.UserInfoId = app.userInfo.Id;
        data.EnterpriseName = enterpriseInfo.componyName;
        data.BusinessLicense = enterpriseInfo.businessLicense;
        data.Industry = enterpriseInfo.industry;
        data.Scale = enterpriseInfo.scale;
        data.FinancingStage = enterpriseInfo.financingStage;
        data.CertifierName = this.data.authName;
        data.CertifierPosition = this.data.authJob;
        data.CertifierIdNumber = this.data.authIdNum;
        data.CertifierFrontPhoto = this.data.authphoto[0];
        data.CertifierBackPhoto = this.data.authphoto[1];
        data.VerifyStatus = '0';
        data.CertifierHoldingPhoto = '';
        data.VerifyTime = utils.formatTime(new Date());
        data.Reason = '';
        console.log(data);
        return data;
    },
    /**
     * ===================事件处理部分===================
     */

    /**
     * -------------------网络请求部分-------------------
     */
    addEnterpriseInfo(data){
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.addEnterpriseInfo,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log('addEnterpriseInfo success' , res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000){
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 5,
                            })
                        }, 500);
                    }
                    Toast(res.data.Message);
                }else{
                    Toast("提交失败，请检查网络连接！")
                }
            },
            fail: function (res) { console.log('addEnterpriseInfo fail' , res)},
            complete: function(res) {wx.hideLoading()},
        })
    },
    /**
     * -------------------网络请求部分-------------------
     */
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            enterpriseInfo: app.globalData.enterpriseInfo
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})